package com.zt.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.zt.pojo.Contract;
import com.opensymphony.xwork2.ModelDriven;
import com.zt.comm.Const;
import com.zt.page.Page;
import com.zt.service.ContractService;

@Controller("contractAction")
public class ContractAction implements ModelDriven<Contract>,RequestAware,SessionAware{
	private static final long serialVersionUID = 1L;
	@Autowired
	private ContractService contractService;
	private Map<String,Object> requestMap,sessionMap;
	private Contract contract=new Contract();
	private int pageNum;
	public String save(){
		System.out.println("contract="+contract);
		return contractService.addContract(contract);
	}
	public String delete(){
		return contractService.deleteContract(contract);
	}
	public String update(){
		System.err.println("修改页面中contract="+contract);
		return contractService.updateContract(contract);
	}
	public String changSignState(){
		return contractService.changSignState(contract);
	}
	public String findByPage(){
		System.out.println("showPage方法进来了");
		System.out.println("contractService="+contractService);
		String msg=Const.ERROR;
		Page<Contract> page=new Page<Contract>();
		List<Contract> list = new ArrayList<Contract>();
		System.err.println("传过来的："+pageNum);
		page.setPageNo(pageNum);
		System.err.println("pageNum="+page.getPageNo());
		Page<Contract> result = contractService.find(page);
		list=result.getList();
		if(list!=null && list.size()>0){
			int rows=contractService.getTotalNum();
			page.setTotalNum(rows);
			int endPage = page.getEnd();
			int pageNum=page.getPageNo();
			System.out.println("list="+list);
			System.out.println("pageNum="+pageNum);
			System.out.println("endPage="+endPage);
			requestMap.put("list", list);
			requestMap.put("pageNum", pageNum);
			requestMap.put("endPage", endPage);
			msg=Const.SUCCESS;
			System.out.println("=================================================");		
		}else{
			sessionMap.put("errMsg", "Dao层取值为空值");
		}
		return msg;
	}
	public String findByFirstPage(){
		System.out.println("findByFirstPage方法进来了");
		System.out.println("contractService="+contractService);
		String msg=Const.ERROR;
		Page<Contract> page=new Page<Contract>();
		page.setPageNo(page.getHome());
		System.err.println("pageNum="+page.getPageNo());
		List<Contract> list = new ArrayList<Contract>();
		Page<Contract> result = contractService.find(page);
		list=result.getList();
		if(list!=null && list.size()>0){
			for(Contract c:list){
				String sign_date = c.getSign_date();
				System.out.println(sign_date);
			}
			int pageNum=result.getPageNo();
			int rows=contractService.getTotalNum();
			page.setTotalNum(rows);
			int endPage = page.getEnd();
			System.out.println("list="+list);
			System.out.println("pageNum="+pageNum);
			System.out.println("endPage="+endPage);
			requestMap.put("list", list);
			requestMap.put("pageNum", pageNum);
			requestMap.put("endPage", endPage);
			System.out.println("=================================================");		
			msg=Const.SUCCESS;
		}else{
			sessionMap.put("errMsg", "Dao层取值为空值");
		}
		return msg;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.sessionMap=arg0;
		
	}
	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.requestMap=arg0;
	}
	@Override
	public Contract getModel() {
		return contract;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
}
